package com.demo.tests.listeners;

import com.demo.utils.LogUtils;
import com.demo.utils.TestResultHistory;
import com.demo.utils.WeChatUtils;
import org.apache.logging.log4j.Logger;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener {
//    private int totalTests = 0;
    private int passedTests = 0;
    private int failedTests = 0;
    private int skippedTests = 0;
    private int failedButWithinSuccessPercentage = 0;
    private static final Logger log = LogUtils.getLogger();

    @Override
    public void onStart(ITestContext context) {
        log.info("【TestListener】测试套件开始执行: " + context.getName());
    }

    @Override
    public void onFinish(ITestContext context) {
        log.info("【TestListener】测试完成，开始发送微信消息...");
        log.info("【TestListener】总测试数: " + (passedTests+failedTests+skippedTests));
        log.info("【TestListener】通过数: " + passedTests);
        log.info("【TestListener】失败数: " + failedTests);
        log.info("【TestListener】跳过数: " + skippedTests);
        log.info("【TestListener】失败但成功率内的测试数: " + failedButWithinSuccessPercentage);

        // 1. 保存本次测试结果
        TestResultHistory.saveCurrentResult(
                (passedTests+failedTests+skippedTests),
                passedTests,
                failedTests,
                failedButWithinSuccessPercentage,
                context.getSkippedTests().size()
        );

        // 2. 读取上一次测试结果
        String lastResult = TestResultHistory.readLastResult();
        log.info("【TestListener】上一次运行结果:\n" + lastResult);

        // 3. 构建企业微信消息
        String message = String.join("\n",
                "**本次测试完成**",
                "> 测试结果如下:",
                "> 总测试数: " + (passedTests+failedTests+skippedTests),
                "> 通过数: " + passedTests,
                "> 失败数: " + failedTests,
                "> 跳过数: " + skippedTests,
                "> 失败但成功率内的测试数: " + failedButWithinSuccessPercentage,
                "",
                "**上一次运行结果**",
                "> " + lastResult.replace("\n", "\n> ")
        );

        // 4. 发送消息
        WeChatUtils.sendWeChatMessage(message);
    }

    @Override
    public void onTestStart(ITestResult result) {
        log.info("【TestListener】开始执行测试: " + result.getName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        passedTests++;
        log.info("【TestListener】测试通过: " + result.getName());
    }

    @Override
    public void onTestFailure(ITestResult result) {
        failedTests++;
        log.info("【TestListener】测试失败: " + result.getName());
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        skippedTests++;
        log.info("【TestListener】测试跳过: " + result.getName());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        failedButWithinSuccessPercentage++;
        log.info("【TestListener】测试失败但在成功率范围内: " + result.getName());
    }
}